﻿using TbShop.Business.Components;
using TbShop.Entity;
using TbShop.Web.Areas.Admin.Models;
using TbShop.Web.Helpers;
using TbShop.Web.Infrastructure;
using Newtonsoft.Json;
using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TbShop.Web.Areas.Admin.Controllers
{
    public class SystemMessageController : BaseCacheManageController<SystemMessage, SystemMessageViewModel>
    {
        protected ServiceComponents<SystemMessage> systemMessageBC = new ServiceComponents<SystemMessage>();
        public SystemMessageBC msgBC = new SystemMessageBC();

        public SystemMessageController()
            : base("系统提醒", "SystemMessage", "SystemMessage", false, true)
        {

        }
        public override string GetDataList(int pageSize = 0, int pageNumber = 0)
        {
            IPagedList<SystemMessage> data = systemMessageBC.GetAll().ToPagedList(pageNumber, pageSize);
            var meta = data.GetMetaData();
            var counts = meta.TotalItemCount;
            return JsonConvert.SerializeObject(new { total = counts, rows = DataMapperHelper.MapList<SystemMessageViewModel>(data) });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public virtual string showDetail(int id, int versionNumber)
        {
            SystemMessage data = null;
            data = systemMessageBC.Get(id);
            data.IsRead = true;
            data.AdminId = CurrentAdmin.Id;
            var saveResult = systemMessageBC.Save(data);
            if (saveResult != null)
            {
                OperationLogHelper.SaveOperationLog(AdminId, CurrentAdmin.UserName, "查看", "", saveResult.Id.ToString());
            }
            return data.Content;
        }
        [HttpPost]
        public string MessageDataCount()
        {
            var messageCount = msgBC.MessageCount();
            return JsonConvert.SerializeObject(new { messageCount = messageCount });
        }
    }
}